package com.ht.aems.fesmodel.persist;

import com.ht.aems.dbservice.ConnectionFactory;
import com.ht.aems.dbservice.SqlExecutor;
import com.ht.aems.fesmodel.FesPersistence;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @version 1.0
 * @author: Dong Zhukai
 * @Date: 2010-3-18 Time: 10:03:46
 */
public class DataRevert {

    public int getCurrentValue (int nRtu, int nPnt) {
        int value = -1;
        Calendar c = Calendar.getInstance ();
        int year = c.get (Calendar.YEAR);
        int month = c.get (Calendar.MONTH) + 1;
        int day = c.get (Calendar.DAY_OF_MONTH);
        int hour = c.get (Calendar.HOUR_OF_DAY);
        int minute = c.get (Calendar.MINUTE);
        int second = c.get (Calendar.SECOND);
        Connection conn = ConnectionFactory.createConnection ();
        String sql = "select nValue from " + FesPersistence.TABLE_DIS + " where nRtu = " + nRtu +
                " and nPnt = " + nPnt + " and nYear = " + year +
                " and nMonth = " + month + " and nDay = " + day +
                " and nHour = " + hour + " and nMinute = " + minute;
        System.out.println (sql);
        Statement sm = null;
        ResultSet rs = null;
        try {
            if(conn != null) {
                sm = conn.createStatement ();
                rs = SqlExecutor.executeQuery (conn, sql);
                while(rs.next ()) {
                    value = rs.getInt ("nValue");
                }
                rs.close ();
                sm.close ();
            }
        } catch(SQLException e) {
            e.printStackTrace ();
        } finally {
            try {
                if(rs != null)rs.close();
                if(sm != null)sm.close();
            } catch(Exception e) {
                e.printStackTrace ();  //To change body of catch statement use File | Settings | File Templates.
            }
            SqlExecutor.close (conn);
        }
        return value;
    }

    public Double[] get228Points (int nId, Date date) {
        Double[] values = new Double[288];
        Calendar c = Calendar.getInstance ();
        c.setTime (date);
        int hour = c.get (Calendar.HOUR_OF_DAY);
        SimpleDateFormat format = new SimpleDateFormat ("yyyyMMdd");
        String tablename = "alg_" + format.format (date);
        Connection conn = ConnectionFactory.createConnection ();
        try {
            Statement sm = conn.createStatement ();
            for(int i = 0; i < 24; i++) {
                if(i > hour)
                    break;
                for(int j = 0; j < 60; j += 5) {
                    String sql = "select fHour" + i + " from " + tablename +
                            " where nId = " + nId + " and nMinute = " + j;
                    //System.out.println (sql);
                    ResultSet rs = sm.executeQuery (sql);
                    while(rs.next ()) {
                        values[i * 12 + j / 5] = rs.getDouble ("fHour" + i);
                    }
                    rs.close ();
                }
            }
            sm.close ();
        } catch(SQLException e) {
            e.printStackTrace ();
        } finally{
            SqlExecutor.close (conn);
        }
        return values;
    }

    public static void main (String[] args) throws ParseException {
        //DataRevert revert = new DataRevert ();
        //String sdate = "20100302 23:59:00";
        //SimpleDateFormat format = new SimpleDateFormat ("yyyyMMdd HH:mm:ss");
        //Double[] d = revert.get228Points (5, format.parse (sdate));
        //for(Double v : d)
        //    System.out.println (v);
        //
        //
        //int vv = revert.getCurrentValue (4, 1);
        //System.out.println (vv);
    }
}
